<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('商品品牌列表')" />
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-editable-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>选分类：</label>
                                <select name="typeId" class="typeId form-control" onchange="$.table.search()"><option value="">全部</option></select>
                            </li>
                            <li>
                                <label>品牌名：</label>
                                <input type="text" name="brandName" onkeyup="$.table.search()"/>
                            </li>
                            <li>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="location.reload()"><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()">
                    <i class="fa fa-plus"></i> 添加
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: jquery-cxselect-js" />
    <th:block th:include="include :: bootstrap-table-editable-js" />
    <script th:inline="javascript">

        $(function() {
            var options = {
                url: ctx + "baseInfo/brand/list",
                createUrl: ctx + "baseInfo/brand/add",
                updateUrl: ctx + "baseInfo/brand/edit/{id}",
                removeUrl: ctx + "baseInfo/brand/remove",
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                onEditableSave: onEditableSave,
                modalName: "商品品牌",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'brandId',
                    title: '品牌id',
                    visible: false
                },
                {
                    field: 'brandName',
                    align: "center",
                    title: '品牌名',
                    editable : {
                        type : 'text',
                        title : '品牌名',
                        validate : function(value) {
                            if (value.length == 0) {
                                return '品牌名不能为空';
                            }
                        }
                    }
                },
                {
                    field: 'typeName',
                    align: "center",
                    title: '分类'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs ' + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.brandId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        });

        $('#bootstrap-table').on('page-change.bs.table', function (e,number, size) {
            window.scrollTo(0,0);
        });

        //大分类
        var typeParentData = [[${parentType}]];
        $('#formId').cxSelect({
            required: false,
            firstTitle: '全部',
            selects: ['typeId'],
            jsonName: 'name',
            jsonValue: 'id',
            data: typeParentData
        });

        //表格编辑保存
        function onEditableSave(field, row, rowIndex, oldValue, $el) {
            $.ajax({
                type: "post",
                url: ctx + "baseInfo/brand/edit",
                data: {
                    "brandId": row.brandId,
                    "brandName": row.brandName
                },
                success: function(result) {
                    // $.table.refresh('bootstrap-table');
                }
            })
        }
    </script>
</body>
</html>